import http from '../utils/httpRequest.js'

let goodsListTest = genGoodsListTest()

// 获取所有商品
export function getGoodsList(params) {
    http({
        ...params,
        path: "/goods/list",
        testData: goodsListTest,
    })
}

// 添加
export function addGoods(params) {
    http({
        ...params,
        method: "post",
        path: "/goods",
        testData: function () {
            params.data.id = Math.random()
            params.data.goodsCategoryName = "goodsCategoryId=" + params.data.goodsCategoryId
            goodsListTest.push(params.data)
            return true
        },
    })
}

// 更新
export function updateGoods(params) {
    http({
        ...params,
        method: "put",
        path: "/goods/" + params.data.id,
        testData: function () {
            params.data.goodsCategoryName = "goodsCategoryId=" + params.data.goodsCategoryId
            for (let i = 0; i < goodsListTest.length; i++)
                if (goodsListTest[i].id === params.data.id)
                    goodsListTest[i] = params.data
            return true
        },
    })
}

// 删除
export function deleteGoods(params) {
    http({
        ...params,
        method: "delete",
        path: "/goods/" + params.goodsId,
        testData: function () {
            for (let i = 0; i < goodsListTest.length; i++)
                if (goodsListTest[i].id === params.goodsId)
                    goodsListTest.splice(i, 1)
            return true
        },
    })
}

// 生成测试数据
function genGoodsListTest() {
    let randInt = function (min, max) {
        return min + Math.round(Math.random() * max);
    }

    let goodsList = []
    for (let i = 0; i < randInt(80, 150); i++) {
        let categoryId = randInt(1, 20)
        goodsList.push({
            id: Math.random(),
            goodsCategoryId: categoryId,
            goodsCategoryName: "种类-" + categoryId,
            name: "奶茶" + i,
            price: randInt(1, 1000), // 价格：没有主属性的时候该字段生效
            count: randInt(0, 10), // 库存：没有主属性的时候该字段生效
            displayOrder: i,
            image: "https://img0.baidu.com/it/u=3074490370,3840344350&fm=26&fmt=auto",
            status: Math.random() > 0.3 ? 1 : 2,
            description: "description-" + Math.random(),
            propertyList: Math.random() < 0.3 ? [] :
                [
                    { // 主属性
                        name: "属性1",
                        isMain: true,
                        options: function () {
                            let optionList = []
                            for (let k = 0; k < 5; k++) {
                                optionList.push({
                                    name: "选项" + k,
                                    price: randInt(1, 1000),
                                    count: randInt(0, 3),
                                    disabled: Math.random() < 0.3,
                                })
                            }
                            return optionList
                        }()
                    },
                    {
                        name: "属性2", // 非主属性
                        required: Math.random() < 0.5,
                        options: function () {
                            let optionList = []
                            for (let k = 0; k < 5; k++) {
                                optionList.push({
                                    name: "选项" + k,
                                    disabled: Math.random() < 0.3,
                                })
                            }
                            return optionList
                        }()
                    },
                ]
        })
    }
    return goodsList
}
